package org.daidai.abstrusewriting.homepage.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.fragments.BaseFragment;
import org.daidai.abstrusewriting.homepage.classify.adapter.ClassifyAdapter;
import org.daidai.abstrusewriting.homepage.classify.client.ClientAPI;
import org.daidai.abstrusewriting.homepage.classify.model.ClassifyList;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassifyFragment extends BaseFragment {

    private static final String TAG = "ClassifyFragment";
    private View ret;
    private ListView mListView;
    private List<ClassifyList> mList;
    private ClassifyAdapter mAdapter;

    public ClassifyFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        return null;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ret = inflater.inflate(R.layout.fragment_classify, container, false);
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        mListView = (ListView) ret.findViewById(R.id.homepage_classify_listview);
        mList = new ArrayList<>();

        mAdapter = new ClassifyAdapter(mList, getContext());

        mListView.setAdapter(mAdapter);
        ClientAPI.getClassifyList();


        return ret;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetClassifyListListener(List<ClassifyList> mList){
         //Log.d(TAG, "onGetClassifyListListener: "+mList.toString());
        if (mList.get(0) instanceof ClassifyList) {
            mAdapter.addAll(mList);
        }
    }
}
